package com.such.kit.calendar;

import java.text.ParseException;
import java.util.Date;
import com.such.kit.calendar.period.DatePeriod;
import com.such.kit.calendar.period.impl.DayPeriod;
import com.such.kit.calendar.period.impl.HalfYearPeriod;
import com.such.kit.calendar.period.impl.MonthPeriod;
import com.such.kit.calendar.period.impl.QuarterPeriod;
import com.such.kit.calendar.period.impl.TendayPeriod;
import com.such.kit.calendar.period.impl.Week5Period;
import com.such.kit.calendar.period.impl.Week7Period;
import com.such.kit.calendar.period.impl.YearPeriod;
import com.such.kit.validate.ValidateCommons;

/**
 * @作者 SUCH
 * @日期 2017-2-17 下午7:28:15
 * @描述 计算日期频度辅助类
 */
public abstract class DatePeriodHelper {

	/** FREQ_YEAR：年 */
	public static final String FREQ_YEAR = "Y";
	/** FREQ_HALF：半年 */
	public static final String FREQ_HALF = "H";
	/** FREQ_QUARTER：季 */
	public static final String FREQ_QUARTER = "Q";
	/** FREQ_MONTH：月 */
	public static final String FREQ_MONTH = "M";
	/** FREQ_TENDAY：旬（10天） */
	public static final String FREQ_TENDAY = "T";
	/** FREQ_WEEK5：周（周五为最后一天） */
	public static final String FREQ_WEEK5 = "W5";
	/** FREQ_WEEK：周（周日为最后一天） */
	public static final String FREQ_WEEK7 = "W7";
	/** FREQ_DAY：日 */
	public static final String FREQ_DAY = "D";

	/**
	 * <p>方法名：getInstance</p>
	 * <p>描述：获取辅助类实例</p>
	 * @param ferq 频度
	 * @param date Date 类型时间
	 * @param format 日期格式
	 * @return 日期频度
	 * @throws ParseException
	 */
	public static DatePeriod getInstance(String ferq, Date date, String format) throws ParseException {
		DatePeriod period = getPeriod(ferq);
		if (ValidateCommons.isNotEmpty(period)) {
			period.init(date, format);
		}
		return period;
	}

	/**
	 * <p>方法名：getInstance</p>
	 * <p>描述：获取辅助类实例</p>
	 * @param ferq 频度
	 * @param date 字符串类型时间
	 * @param format 日期格式
	 * @return 日期频度
	 * @throws ParseException
	 */
	public static DatePeriod getInstance(String ferq, String date, String format) throws ParseException {
		DatePeriod period = getPeriod(ferq);
		if (ValidateCommons.isNotEmpty(period)) {
			period.init(date, format);
		}
		return period;
	}

	private static DatePeriod getPeriod(String ferq) {
		DatePeriod period = null;
		if (FREQ_YEAR.equals(ferq)) {
			period = new YearPeriod();
		} else if(FREQ_HALF.equals(ferq)) {
			period = new HalfYearPeriod();
		} else if(FREQ_QUARTER.equals(ferq)) {
			period = new QuarterPeriod();
		} else if(FREQ_MONTH.equals(ferq)) {
			period = new MonthPeriod();
		} else if(FREQ_TENDAY.equals(ferq)) {
			period = new TendayPeriod();
		} else if(FREQ_WEEK5.equals(ferq)) {
			period = new Week5Period();
		} else if(FREQ_WEEK7.equals(ferq)) {
			period = new Week7Period();
		} else if(FREQ_DAY.equals(ferq)) {
			period = new DayPeriod();
		}
		return period;
	}

}
